# Chess Project Makefile

CC = gcc
CFLAGS = -Wall -g -std=c99
LIBS = -lncursesw -lncurses -lsqlite3 -lpthread -lm

# 目录
CLIENT_DIR = client
SERVER_DIR = server
COMMON_DIR = common
CHESS_LOGIC_DIR = chess_logic
UI_DIR = ui
BIN_DIR = bin

# 源文件
CLIENT_SRCS = $(CLIENT_DIR)/client.c
SERVER_SRCS = $(SERVER_DIR)/server.c
COMMON_SRCS = $(COMMON_DIR)/protocol.c
CHESS_LOGIC_SRCS = $(CHESS_LOGIC_DIR)/chess_logic.c
UI_SRCS = $(UI_DIR)/ui.c

# 头文件
CLIENT_HEADERS = $(CLIENT_DIR)/client.h
SERVER_HEADERS = $(SERVER_DIR)/server.h
COMMON_HEADERS = $(COMMON_DIR)/common.h $(COMMON_DIR)/protocol.h
CHESS_LOGIC_HEADERS = $(CHESS_LOGIC_DIR)/chess_logic.h
UI_HEADERS = $(UI_DIR)/ui.h

# 目标文件
CLIENT_OBJS = $(CLIENT_SRCS:.c=.o)
SERVER_OBJS = $(SERVER_SRCS:.c=.o) $(COMMON_SRCS:.c=.o)
COMMON_OBJS = $(COMMON_SRCS:.c=.o)
CHESS_LOGIC_OBJS = $(CHESS_LOGIC_SRCS:.c=.o)
UI_OBJS = $(UI_SRCS:.c=.o)

# 可执行文件
CLIENT_EXE = $(BIN_DIR)/chess_client
SERVER_EXE = $(BIN_DIR)/chess_server

# 默认目标
all: $(CLIENT_EXE) $(SERVER_EXE)

# 客户端
$(CLIENT_EXE): $(CLIENT_OBJS) $(COMMON_OBJS) $(CHESS_LOGIC_OBJS) $(UI_OBJS)
	@mkdir -p $(BIN_DIR)
	$(CC) $(CFLAGS) -o $@ $^ $(LIBS)

# 服务器
$(SERVER_EXE): $(SERVER_OBJS) $(COMMON_OBJS)
	@mkdir -p $(BIN_DIR)
	$(CC) $(CFLAGS) -o $@ $^ $(LIBS)

# 编译客户端源文件
$(CLIENT_DIR)/%.o: $(CLIENT_DIR)/%.c $(CLIENT_HEADERS) $(COMMON_HEADERS) $(UI_HEADERS) $(CHESS_LOGIC_HEADERS)
	$(CC) $(CFLAGS) -c -o $@ $<

# 编译服务器源文件
$(SERVER_DIR)/%.o: $(SERVER_DIR)/%.c $(SERVER_HEADERS) $(COMMON_HEADERS)
	$(CC) $(CFLAGS) -c -o $@ $<

# 编译通用源文件
$(COMMON_DIR)/%.o: $(COMMON_DIR)/%.c $(COMMON_HEADERS)
	$(CC) $(CFLAGS) -c -o $@ $<

# 编译游戏逻辑源文件
$(CHESS_LOGIC_DIR)/%.o: $(CHESS_LOGIC_DIR)/%.c $(CHESS_LOGIC_HEADERS)
	$(CC) $(CFLAGS) -c -o $@ $<

# 编译UI源文件
$(UI_DIR)/%.o: $(UI_DIR)/%.c $(UI_HEADERS) $(CHESS_LOGIC_HEADERS)
	$(CC) $(CFLAGS) -c -o $@ $<

# 清理
clean:
	rm -f $(CLIENT_OBJS) $(SERVER_OBJS) $(COMMON_OBJS) $(CHESS_LOGIC_OBJS) $(UI_OBJS)
	rm -rf $(BIN_DIR)

# 安装依赖 (Ubuntu)
install-deps:
	sudo apt-get install libncurses5-dev libncursesw5-dev libsqlite3-dev

.PHONY: all clean install-deps
